﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
using DAL;
namespace DAL
{
  public  class StudentService
    {
      //学号查询学生
      public Student GetStudentBySno(string sno, List<Student> ls) {
          Student s = new Student();

          foreach (Student stu in ls) {
              if (stu.Sno.Equals(sno)) {
                  s = stu;
              }
          }

          return s;
      }
      //多条件查询
      public List<Student> GetAllByItem(string sno,string name,string phone ,List<Student> ls)
      {
          List<Student> qs = new List<Student>();
          foreach (Student s in ls)
          {
              if (s.Sno.StartsWith(sno))
              {
                  qs.Add(s);
              }
          }

          return qs;
      }

      //学号查询学生列表
      public List<Student> GetAllBySno(string sno, List<Student> ls) {
          List<Student> qs = new List<Student>();
          foreach (Student s in ls) {
              if (s.Sno.StartsWith(sno)) {
                  qs.Add(s);
              }
          }

          return qs;
      }
      //姓名查询学生列表
      public List<Student> GetAllByName(string sname, List<Student> ls)
      {
          List<Student> qs = new List<Student>();
          foreach (Student s in ls)
          {
              if (s.Sname.StartsWith(sname))
              {
                  qs.Add(s);
              }
          }

          return qs;
      }
      //电话查询学生列表
      public List<Student> GetAllByPhone(string phone, List<Student> ls)
      {
          List<Student> qs = new List<Student>();
          foreach (Student s in ls)
          {
              if (s.Phone.StartsWith(phone))
              {
                  qs.Add(s);
              }
          }

          return qs;
      }
      //新增学生
      public void saveStudnet(Student s, List<Student> ls) {
          try
          {
              ls.Add(s);
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
      }
      //更新信息
      public void UpdateStudent(Student s, List<Student> ls) {
          try
          {
              for (int i = 0; i < ls.Count; i++) { 
                if(ls[i].Sno.Equals(s.Sno)){
                    ls.RemoveAt(i);
                    ls.Insert(i, s);
                    break;
                }
              }

          }
          catch (Exception ex)
          {
              
              throw ex;
          }
      }
      //删除信息
      public void delStudent(String sno, List<Student> ls) {
          try
          {
              for (int i = 0; i < ls.Count; i++) { 
                if(ls[i].Sno.Equals( sno)){
                    ls.RemoveAt(i);
                   
                    break;
                }
              }
          }
          catch (Exception ex)
          {
              
              throw ex;
          }
      }
      public string PhotoPath(string pp) {
          string pn = DateTime.Now.ToString("yymmddHHmmss");
          Random rd = new Random();
          pn+=rd.Next(0,99).ToString("00");
          pn += pp.Substring(pp.Length - 4);
          return pn;
      }

    }
}
